package x.y.z;

public class Interview_1_3_URLify {
    public String replaceSpaces(String S, int length) {
        int spaceCount = 0;
        for (int i = 0; i < length; i++) {
            if (S.charAt(i) == ' ') {
                spaceCount++;
            }
        }
        int index = length + spaceCount * 2;
        char[] chs = new char[index];
        for (int i = length - 1; i >= 0; i--) {
            if (S.charAt(i) == ' ') {
                chs[index - 1] = '0';
                chs[index - 2] = '2';
                chs[index - 3] = '%';
                index = index - 3;
            } else {
                chs[index - 1] = S.charAt(i);
                index--;
            }
        }
        return new String(chs);
    }
}